%{
Michael Posner
Nicholas Bartlett
Neel Doshi
CIS 520
Final Project
method examples
%}

load music_dataset.mat

[Xt_lyrics] = make_lyrics_sparse(train, vocab);
[Xq_lyrics] = make_lyrics_sparse(quiz, vocab);

Yt = zeros(numel(train), 1);
for i=1:numel(train)
    Yt(i) = genre_class(train(i).genre);
end

Xt_audio = make_audio(train);
Xq_audio = make_audio(quiz);

%%
%Stemming example:
%  takes in the NxM example matrix and the vocab
%  corresponding to the word counts and returns
%  a matrix where features corresponding to 
%  words with the same stem have been combined
Xt_lyrics = stemmer(Xt_lyrics,vocab);

%%
%Cross validate to find optimal C value for SVM

params = [0.001 0.01 0.1 1 10 100 500 1000 5000 10000]';

%scale features to [0,1] (from libsvm)
Xt_lyrics = (Xt_lyrics - repmat(min(Xt_lyrics,[],1),size(Xt_lyrics,1),1))* ...
    spdiags(1./(max(Xt_lyrics,[],1)-min(Xt_lyrics,[],1))',0,size(Xt_lyrics,2),size(Xt_lyrics,2));

[opt_c error_vect std_vect] = cv_wrapper_svm( Xt_lyrics, Yt, params, 'intersection');

save opt_c

%%
%Cross validate to find optimal depth for DTs
params = (1:1:10)'; 
opt_depth_dt = cv_wrapper_dt(Xt_audio, Yt, params); 

%%
%Run DTs on audio example:
dt = dt_train_multi(Xt_audio, Yt, opt_depth_dt); 
dt_scores = dt_test_multi(dt, Xt_audio);
dt_ranks = get_ranks(dt_scores);

%% 
%Boost a pool of "weak learner" DT's with adaboost:
T = 10;                                 % number of rounds of boosting 
depth_limit = 6;                        % depth limit on trees
% train adaboost for T rounds with depth_limt DT
boost = adaboost_train(Xt_audio, Yt, T, depth_limit); 
boost_scores = adaboost_test(boost, Xq_audio); 
boost_ranks = get_ranks(boost_scores); 

%%
%Naive Bayes on audio example:

nb = NaiveBayes.fit(Xt_audio,Yt,'Distribution','kernel');
nb_scores = posterior(nb,Xq_audio);
nb_ranks = get_ranks(nb_scores);

% see naive_bayes.m for an example of cross validation

%% Save results to a text file for submission
save('-ascii', 'submit_dt.txt', 'dt_ranks');
save('-ascii', 'submit_boost.txt', 'boost_ranks');
save('-ascii', 'submit_nb.txt', 'nb_ranks');